/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entity.Orders;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author shyfu_000
 */
@Stateless
public class OrdersFacade extends AbstractFacade<Orders> implements OrdersFacadeLocal {

    @PersistenceContext(unitName = "Delta-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public OrdersFacade() {
        super(Orders.class);
    }

    @Override
    public int setDelivered(int idOrder) {
        Orders order = find(idOrder);
        if (order != null) {
            if (order.getOrderStatus().equalsIgnoreCase("delivered")) {
                return 0;
            }else {
                order.setDeliveryDate(new java.util.Date());
                order.setOrderStatus("delivered");
                edit(order);
                return 1;
            }
        } else return 2;
    }

    /**
     *
     * @param range
     * @param status
     * @param sortBy
     * @return
     */
    @Override
    public List<Orders> findRangeSortBy(int[] range, String status, String sortBy) {
       return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy"+sortBy).setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();
//        if(sortBy.equalsIgnoreCase("idOrder")) return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy").setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();
//        if(sortBy.equalsIgnoreCase("idOrder")) return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy").setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();
//        if(sortBy.equalsIgnoreCase("idOrder")) return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy").setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();
//        if(sortBy.equalsIgnoreCase("idOrder")) return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy").setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();
//        if(sortBy.equalsIgnoreCase("idOrder")) return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy").setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();
//        if(sortBy.equalsIgnoreCase("idOrder")) return getEntityManager().createNamedQuery("Orders.findByOrderStatusSortBy").setParameter("orderStatus",status).setFirstResult(range[0]).setMaxResults(range[1] - range[0]).getResultList();

    }

    @Override
    public int count(String status) { 
        return Integer.parseInt(getEntityManager().createNamedQuery("Orders.countByStatus").setParameter("orderStatus",status).getSingleResult().toString());

    }
    
}
